TC_BLOCK_CONFIG = {
	TYPE:'',
	BINDING_URI: base_url + 'cms/cp/block/bindingbytype/',
	EDITPANEL_URI:base_url + 'cms/cp/block/editpanel/',
	COMMIT_URI:base_url + 'cms/cp/block/oncommit/',
	DELETE_URI:base_url + 'cms/cp/block/ondelete/'
};
var TC_BLOCK = (function() {
	
    var oTable;
    var that = this;
    return {
        createtable :   function(){
             oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [
                    {
                        mData: "block_id",sWidth: "40px",sClass:'gridAction',
                        mRender: function ( value, type, datarow ) {
                            var str = '';
							str += '<ul class="table-controls">';
							//str += '<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
							str += 	'<li><a href="#" class="tip" onclick="TC_BLOCK.onEditItem(' + value + ')" title="Edit entry" ><i class="icon-pencil"></i></a> </li>';
							if(	datarow.block_lock!=1)
								str += 	'<li><a href="#" class="tip" onclick="TC_BLOCK.onDeleteItem(' + value + ')" title="Remove entry" ><i class="icon-trash"></i></a> </li>';
                            str += '</ul>';
                            return str;
                        }
                    },
                    {
                        mData: "block_title"
                    },
                    {
                        mData: "block_position",sWidth: "60px"
                    },
                    {mData: "block_insert",sWidth: "120px"},
                    {mData: "block_update",sWidth: "120px"},
                    {mData: "block_lock",sWidth: "40px",bVisible:false}
                ],
                sServerMethod: "POST",
                iDisplayLength: 50,
                bProcessing: true,
                bServerSide: true,
                bPaginate: false,
                sAjaxSource: TC_BLOCK_CONFIG.BINDING_URI+TC_BLOCK_CONFIG.TYPE,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {"sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<"}
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit  :   function(){
            this.createtable();
        },
        onAddItem:function(){
            this.onEditItem(0);
        },
        onEditItem:function(Id){
            if (PENDING)
                return false;
            $('#dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title:'Add/Edit Block',
                message:$('#dialog-edit-panel'),
                buttons: [
                    {
                        text    :'Done',
                        class   :'base_button base_green base-small-border-radius',
                        click   : function() {
                            TC_BLOCK.onSave();
                        }
                    },
                    {
                        text    :'Close',
                        class   :'base_button base_red base-small-border-radius',
                        click   : function() {
                            $(this).dialog("close");
                        }
                    }
                ]
            }).open();
            backend({
                url         :   TC_BLOCK_CONFIG.EDITPANEL_URI+TC_BLOCK_CONFIG.TYPE,
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        $('#dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">'+rsdata.message+'</span>');
                    }else{
                        $('#dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
                        $('#dialog-edit-panel').dialog('close').dialog('open'); 
						$('#_form_edit_panel select').select2({minimumResultsForSearch :-1});
                    }
                }
            }).call();
        },
        onSave:function(){
            if( $('#_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#_form_edit_panel').serializeObject();
            $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
            new backend({
                url         :   TC_BLOCK_CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   Params,
                    Id      :   $('#Id').val()
                },
                callback    :   function(rsdata){
                    $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }else{
                        TC_BLOCK.onRefresh();
                        TC_BLOCK.onCancel();
                    }
                    
                }
            }).call();
            return false;
        },
        onCancel:function(){
            $('#dialog-edit-panel').dialog('close');
        },
        onDeleteItem:function(Id){
            backend_delete(TC_BLOCK_CONFIG.DELETE_URI,Id,function(){
                TC_BLOCK.onRefresh();
            });
        },
        onRefresh:function(){
            oTable.fnDraw(false);
        },
        onFillter:function(v){
            oTable.fnFilter(v, 2);
        },
        onStatusChange:function(that,Id){
            var _status = $(that).is(':checked')?'true':'false';
            new backend({
                url         :   TC_BLOCK_CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   {
                        staf_status:_status
                    },
                    Id      :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }
                }
            }).call();
        },
		onChangeSource : function(type){
			if(type!==TC_BLOCK_CONFIG.TYPE){
				TC_BLOCK_CONFIG.TYPE=type;
				oTable.fnSettings().sAjaxSource = (TC_BLOCK_CONFIG.BINDING_URI+TC_BLOCK_CONFIG.TYPE);
				oTable.fnDraw(false);
			}
		}
    };
}());